/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.service;

import info.peigen.hotpot.business.account.facade.AccountTransServiceFacade;
import info.peigen.hotpot.business.account.facade.order.trans.TransferOrder;
import info.peigen.hotpot.business.account.facade.result.trans.TransferResult;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceInvoke;
import org.noear.solon.annotation.Component;


/**
 * <b>(AccountTransService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/27
 */
@SuppressWarnings("rawtypes")
@Component
public class AccountTransService extends AbstractAccountService implements AccountTransServiceFacade {

    @Override
    public TransferResult deposit(TransferOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new TransferResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeDepositAccountProcessor, activeFeeAccountProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(transferAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeAccountTransferProcessor);
            }
        });
        return (TransferResult) serviceContext.getResult();
    }

    @Override
    public TransferResult transfer(TransferOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new TransferResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeTransferAccountProcessor, activeFeeAccountProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(transferAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeAccountTransferProcessor, transferAccountResultProcessor);
            }
        });
        return (TransferResult) serviceContext.getResult();
    }

    @Override
    public TransferResult withdraw(TransferOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new TransferResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeWithdrawAccountProcessor, activeFeeAccountProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(transferAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeAccountTransferProcessor);
            }
        });
        return (TransferResult) serviceContext.getResult();
    }
}
